package com.pyue.eam.admin.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pyue.eam.admin.mapper.EamAreaMapper;
import com.pyue.eam.common.core.util.R;
import com.pyue.eam.common.security.annotation.Inner;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@AllArgsConstructor
@RequestMapping("/eamArea")
public class EamAreaController {
    private final EamAreaMapper eamAreaMapper;

    /**
     * 查询所有的城市数据树
     */
    @Inner(value = false)
    @GetMapping("/cityDataTree")
    public R<List<Tree<Integer>>> cityDataTree() {
        final List<TreeNode<Integer>> treeNodeList = eamAreaMapper.selectList(Wrappers.emptyWrapper()).stream()
                .filter(area -> !area.getCodeId().equals(area.getParentId()))
                .map(area -> {
                    TreeNode<Integer> node = new TreeNode<>();
                    node.setId(area.getCodeId());
                    node.setParentId(area.getParentId());
                    node.setWeight(area.getCodeId());
                    node.setName(area.getCityName());
                    return node;
                }).collect(Collectors.toList());

        return R.okList(TreeUtil.build(treeNodeList, 100000));
    }

}
